"""
Case Type   : 资源池化双集群[stream]
Case Name   : 主、备集群均空载, 备集群开启极致RTO并重启，主集群重启
Create At   : 2025-05-14
Owner       : jingtao
Description :
    1. 查看主、备集群状态
    2. 备集群开启极致RTO并关闭 hot_standby
    3. 主、备集群重启，查看主、备集群状态
    4. 恢复集群为正常状态(参数恢复)
Expect      :
    1. 集群状态正常
    2. 成功
    3. 集群状态正确
History     :
    1. 2025-05-14 创建
"""
import os
import unittest
import time
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import macro
logger = Logger()
P_Primary_SH = CommonSH('P_PrimaryDbUser')
S_Primary_SH = CommonSH('S_PrimaryDbUser')

@unittest.skipIf(1 == P_Primary_SH.exec_cm_ctl('list', '--param --server | grep ss_double_cluster_mode').split('=')[-1].strip(), '非双集群环境下不执行')
class Dual_Cluster_NormalCase_Case0002(unittest.TestCase):

    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()

    def test_shared_storage(self):
        text = '-----step1:查看主备集群状态; expect:集群状态正常-----'
        logger.info(text)
        status = P_Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        status = S_Primary_SH.exec_cm_ctl(mode='query', param='-Cv', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        status = P_Primary_SH.check_data_consistency()
        self.assertTrue(status, '容灾关系断开' + text)

        text = '-----step2:备集群开启极致RTO并关闭hot_standby-----'
        logger.info(text)
        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                            'recovery_parse_workers=2', env_path=macro.DB_STANDBY_ENV_PATH)
        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                            'recovery_redo_workers=2', env_path=macro.DB_STANDBY_ENV_PATH)
        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                            'hot_standby=off', env_path=macro.DB_STANDBY_ENV_PATH)

        text = '-----step3:主、备集群重启, 并查看主备集群状态; expect:执行成功,集群状态符合预期-----'
        logger.info(text)
        """主集群重启"""
        stop = P_Primary_SH.exec_cm_ctl(mode='stop')
        logger.info(stop)
        self.assertIn('successfully', stop, '执行失败' + text)
        start = P_Primary_SH.exec_cm_ctl(mode='start')
        logger.info(start)
        self.assertIn('successfully', start, '执行失败' + text)
        """备集群重启"""
        stop = S_Primary_SH.exec_cm_ctl(mode='stop', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(stop)
        self.assertIn('successfully', stop, '执行失败' + text)
        start = S_Primary_SH.exec_cm_ctl(mode='start', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(start)
        self.assertIn('successfully', start, '执行失败' + text)
        status = S_Primary_SH.exec_cm_ctl(mode='query', param='-Cv', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        res1 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        res2 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        logger.info(res1)
        logger.info(res2)
        self.assertIn('P Main Standby Normal', res1, '执行失败' + text)
        self.assertIn('S Standby Normal', res2, '执行失败' + text)
        status = P_Primary_SH.check_data_consistency()
        self.assertTrue(status, '容灾关系断开' + text)

    def tearDown(self):
        text = '-----step4:恢复环境为初始集群状态; expect:成功-----'
        logger.info(text)

        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                             'recovery_parse_workers=1', env_path=macro.DB_STANDBY_ENV_PATH)
        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                             'recovery_redo_workers=1',  env_path=macro.DB_STANDBY_ENV_PATH)
        S_Primary_SH.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                             'hot_standby=on', env_path=macro.DB_STANDBY_ENV_PATH)
        
        status = S_Primary_SH.exec_cm_ctl(mode='query', param='-Cv', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        status = S_Primary_SH.exec_cm_ctl(mode='stop', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        self.assertIn('successfully', status, '执行失败' + text)

        status = S_Primary_SH.exec_cm_ctl(mode='start', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        self.assertIn('successfully', status, '执行失败' + text)

        status = S_Primary_SH.exec_cm_ctl(mode='query', param='-Cv', env=macro.DB_STANDBY_ENV_PATH)
        logger.info(status)
        self.assertIn('P Main Standby Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        status = P_Primary_SH.check_data_consistency()
        self.assertTrue(status, '容灾关系断开' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')